﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Net.Http;
using HtmlAgilityPack;
using System.Diagnostics;

namespace PictureCatch
{
    class Program
    {
        //设置多线程的任务是为了下载和 解析出img路径（待测试，看看效率）


        static WebClient client = new WebClient();
        readonly static string resURL = @"http://jandan.net/ooxx/";
        static BlockingCollection<string> imgQueue = new BlockingCollection<string>(100);//存入待下载的img地址

        static void Main(string[] args)
        {
            
            client.Headers.Add("Accept-Language", "zh-cn");
            client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");
            Debug.Print("====开始抓取首页");
            Byte[] pageData = client.DownloadData(resURL);
            string pageHtml = Encoding.UTF8.GetString(pageData);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(pageHtml);
            HtmlNode hn = doc.DocumentNode.SelectSingleNode("//span[@class='current-comment-page']");
            int currentPage = Convert.ToInt32(hn.InnerText.Replace("[", "").Replace("]", ""));
            Debug.Print("首页结束，开始执行多线程抓取");

            Task.Run(() =>
            {
                Parallel.For(1, currentPage, i =>
                    {
                        string html = DownloadHtml(resURL + "page-" + i);
                        
                    });
            });
            while (!imgQueue.IsCompleted)
            {

            }

            //Task.Run(() =>
            //{
            //    Parallel.For(1, currentPage, i =>
            //        {
            //            if (imgQueue.IsCompleted)
            //            {

            //            }
            //        });
            //});


            

        }

        private static string DownloadHtml(string resURL)
        {
            Byte[] pageData = client.DownloadData(resURL);
            return Encoding.UTF8.GetString(pageData);
        }

    }
}
